/* Andreas Baum
 * IMClientApp.java
 * Handles the connection of an IM client to the IM server
 * CS 78 - Networks - Programming Assignment 1
 */


package ChatClientApplet;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;



public class LoginPanel extends JPanel implements ActionListener {

	


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel window;
	private JTextArea mainTitle;
	private static JLabel instructionLabel;
	private static JLabel errorLabel;
	private static JLabel connectedLabel;
	private JTextField inputField;
	private JButton connectButton;
	private Boolean isDone;
	private String username;
	
	public LoginPanel() {
		isDone = false;
		init();
	}

	public void init() {
		window = new JPanel(new GridBagLayout());
		
		window.setBackground(Color.white);
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.ipadx = 10;

		mainTitle = new JTextArea("Welcome to the CH@TROOM");
		mainTitle.setAlignmentX(CENTER_ALIGNMENT);
		mainTitle.setEditable(false);
		constraints.gridx = 0;
		constraints.gridy = 0;
		window.add(mainTitle, constraints);

		instructionLabel = new JLabel("Please enter your username:");
		instructionLabel.setAlignmentX(CENTER_ALIGNMENT);
		constraints.gridx = 0;
		constraints.gridy = 1;
		window.add(instructionLabel, constraints);

		inputField = new JTextField("<username>");
		inputField.setEnabled(false);
		inputField.setColumns(10);
		inputField.addActionListener(this);
		constraints.gridx = 0;
		constraints.gridy = 2;
		window.add(inputField, constraints);

		connectButton = new JButton("Log in");
		connectButton.setAlignmentX(CENTER_ALIGNMENT);
		connectButton.addActionListener(this);
		constraints.gridx = 0;
		constraints.gridy = 3;
		window.add(connectButton, constraints);
		
		errorLabel = new JLabel("");
		errorLabel.setForeground(Color.red);
		errorLabel.setAlignmentX(CENTER_ALIGNMENT);
		constraints.gridx = 0;
		constraints.gridy = 4;
		window.add(errorLabel, constraints);
		

		connectedLabel = new JLabel("Not connected");
		connectedLabel.setForeground(Color.red);
		connectedLabel.setAlignmentX(CENTER_ALIGNMENT);
		constraints.gridx = 0;
		constraints.gridy = 5;
		window.add(connectedLabel, constraints);
		
		add(window);
	}

	public void actionPerformed(ActionEvent e) {
		username = inputField.getText();
		isDone = true;
	}
	
	public void setConnected(String string, Color color) {
		connectedLabel.setText(string);
		connectedLabel.setForeground(color);
	}
	
	public void enableInput(Boolean bool) {
		inputField.setEnabled(bool);
	}

	public boolean getDone() {
		return isDone;
	}
	
	public void setError(String string) {
		isDone = false;
		errorLabel.setText(string);
	}
	
	public String getUserName() {
		return username;
	}
}